<template>
  <a-config-provider :locale="locale">
    <a-card :bordered="false" class="card-area">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <!-- 搜索区域 -->
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="24">
              <a-form-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-range-picker
                  v-model="queryParam.createTimeRange"
                  format="YYYY-MM-DD"
                  :placeholder="['开始时间', '结束时间']"
                  @change="onDateChange"
                  @ok="onDateOk"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" v-if="orgaTree.length">
              <a-form-item label="机构" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-tree-select style="width:100%" allow-clear :treeData="orgaTree"
                               v-model="queryParam.organizationId" placeholder="请选择机构">
                </a-tree-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery">查询</a-button>
                <a-button style="margin-left: 8px" v-print="'#reportPrint'" icon="printer">打印</a-button>
                <a-button style="margin-left: 8px" @click="exportExcel" icon="download">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- table区域-begin -->
      <div style="width:100%; min-width:1550px">
        <section ref="print" id="reportPrint">
          <a-table
            bordered
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :components="handleDrag(columns)"
            :pagination="false"
            :loading="loading"
            @change="handleTableChange">
            <span slot="customTitle">
              <a-popover trigger="click" placement="right">
                <template slot="content">
                  <a-checkbox-group @change="onColChange" v-model="settingDataIndex" :defaultValue="settingDataIndex">
                    <a-row style="width: 600px">
                      <template v-for="(item,index) in defColumns">
                        <template>
                          <a-col :span="6">
                            <a-checkbox :value="item.dataIndex" v-if="item.dataIndex==='rowIndex'" disabled></a-checkbox>
                            <a-checkbox :value="item.dataIndex" v-if="item.dataIndex!=='rowIndex'">
                              <j-ellipsis :value="item.title" :length="10"></j-ellipsis>
                            </a-checkbox>
                          </a-col>
                        </template>
                      </template>
                    </a-row>
                    <a-row style="padding-top: 10px;">
                      <a-col>
                        恢复默认列配置：<a-button @click="handleRestDefault" type="link" size="small">恢复默认</a-button>
                      </a-col>
                    </a-row>
                  </a-checkbox-group>
                </template>
                <a-icon type="setting" />
              </a-popover>
            </span>
          </a-table>
          <a-row :gutter="24" style="margin-top: 8px;text-align:right;">
            <a-col :md="24" :sm="24">
              <a-pagination @change="paginationChange" @showSizeChange="paginationShowSizeChange"
                size="small"
                show-size-changer
                :showQuickJumper="true"
                :page-size="ipagination.pageSize"
                :page-size-options="ipagination.pageSizeOptions"
                :total="ipagination.total"
                :show-total="(total, range) => `共 ${total-parseInt(total/ipagination.pageSize)-1} 条`">
                <template slot="buildOptionText" slot-scope="props">
                  <span>{{ props.value-1 }}条/页</span>
                </template>
              </a-pagination>
            </a-col>
          </a-row>
        </section>
      </div>
      <!-- table区域-end -->
    </a-card>
  </a-config-provider>
</template>
<script>
  import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { updateTheme } from '@/components/tools/setting'
  import { DEFAULT_COLOR } from "@/store/mutation-types"
  import { getAllOrganizationTreeByUser} from '@/api/api'
  import { getFormatDate, getNowFormatYear, getPrevMonthFormatDate } from '@/utils/util'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import Vue from 'vue'
  import moment from 'moment'
  export default {
    name: "SaleRatioStatistics",
    mixins:[JeecgListMixin],
    components: {
      JEllipsis
    },
    data () {
      return {
        locale: zhCN,
        // 查询条件
        queryParam: {
          beginTime: getPrevMonthFormatDate(3),
          endTime: getFormatDate(),
          createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())],
          organizationId: undefined
        },
        ipagination:{
          pageSize: 11,
          pageSizeOptions: ['11', '21', '31', '101', '201']
        },
        depotList: [],
        orgaTree: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        pageName: 'saleRatioStatistics',
        // 默认索引
        defDataIndex:['rowIndex','mName','categoryName','allNum','allPrice','ratio'],
        // 默认列
        defColumns: [
          {
            dataIndex: 'rowIndex', width:40, align:"center", slots: { title: 'customTitle' },
            customRender:function (t,r,index) {
              return (t !== '合计') ? (parseInt(index) + 1) : t
            }
          },
          {title: '商品名称', dataIndex: 'mName', width: 200},
          {title: '类别名称', dataIndex: 'categoryName', width: 120},
          {title: '销售数量', dataIndex: 'allNum', sorter: (a, b) => a.allNum - b.allNum, width: 120},
          {title: '销售金额', dataIndex: 'allPrice', sorter: (a, b) => a.allPrice - b.allPrice, width: 120},
          {title: '销售占比', dataIndex: 'ratio', width: 120}
        ],
        url: {
          list: "/api/plugin/sale-ratio-statistics/saleRatioStatistics/getList"
        }
      }
    },
    created() {
      this.loadAllOrgaData()
      this.initColumnsSetting()
      this.defaultTimeStr = [moment(getNowFormatYear() + '-01-01', this.dateFormat), moment(this.currentDay, this.dateFormat)]
    },
    mounted () {
      this.primaryColor = Vue.ls.get(DEFAULT_COLOR)
      updateTheme(this.primaryColor)
    },
    methods: {
      moment,
      loadAllOrgaData(){
        let that = this
        let params = {}
        getAllOrganizationTreeByUser(params).then((res)=>{
          if(res){
            that.orgaTree = res
          }
        })
      },
      onDateChange: function (value, dateString) {
        this.queryParam.beginTime=dateString[0]
        this.queryParam.endTime=dateString[1]
        if(dateString[0] && dateString[1]) {
          this.queryParam.createTimeRange = [moment(dateString[0]), moment(dateString[1])]
        }
      },
      onDateOk(value) {
        console.log(value);
      },
      exportExcel() {
        let list = []
        let head = '商品名称,类别名称,销售数量,销售金额,销售占比'
        for (let i = 0; i < this.dataSource.length; i++) {
          let item = []
          let a = this.dataSource[i]
          item.push(a.mName, a.categoryName, a.allNum, a.allPrice, a.ratio)
          list.push(item)
        }
        let tip = '单据日期：' + this.queryParam.beginTime + '~' + this.queryParam.endTime
        this.handleExportXlsPost('销售占比', '销售占比', head, tip, list)
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>